//
// ParameterDefinition.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// (C) 2005 - 2008 Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;

namespace Mono.Cecil {

	public sealed class ParameterDefinition : ParameterReference, /*IHasMarshalSpec,*/
		ICustomAttributeProvider, IConstantProvider {

		ParameterAttributes attributes;

		object constant = Constants.NotResolved;

		CustomAttributeCollection custom_attributes;

		//MarshalSpec m_marshalDesc;

		public ParameterAttributes Attributes {
			get { return attributes; }
			set { attributes = value; }
		}

		public bool HasConstant {
			get {
				ResolveConstant ();

				return constant != Constants.NoValue;
			}
			set { if (!value) constant = Constants.NoValue; }
		}

		public object Constant {
			get { return HasConstant ? constant : null;	}
			set { constant = value; }
		}

		void ResolveConstant ()
		{
			if (constant != Constants.NotResolved)
				return;

			if (!HasImage) {
				constant = Constants.NoValue;
				return;
			}

			constant = parameter_type.Module.Read (this, (field, reader) => reader.ReadConstant (field));
		}

		internal bool HasImage {
			get { return parameter_type != null && parameter_type.Module != null && parameter_type.Module.HasImage; }
		}

		public bool HasCustomAttributes {
			get {
				if (custom_attributes != null)
					return custom_attributes.Count > 0;

				if (HasImage)
					return ParameterType.Module.Read (this, (type, reader) => reader.HasCustomAttributes (type));

				return false;
			}
		}

		public CustomAttributeCollection CustomAttributes {
			get {
				if (custom_attributes != null)
					return custom_attributes;

				if (HasImage)
					return custom_attributes = ParameterType.Module.Read (this, (type, reader) => reader.ReadCustomAttributes (type));

				return custom_attributes = new CustomAttributeCollection ();
			}
		}

		/*public MarshalSpec MarshalSpec {
			get { return m_marshalDesc; }
			set {
				m_marshalDesc = value;
				if (value != null)
					m_attributes |= ParameterAttributes.HasFieldMarshal;
				else
					m_attributes &= ~ParameterAttributes.HasFieldMarshal;
			}
		}*/

		#region ParameterAttributes

		public bool IsIn {
			get { return (attributes & ParameterAttributes.In) != 0; }
			set {
				if (value)
					attributes |= ParameterAttributes.In;
				else
					attributes &= ~ParameterAttributes.In;
			}
		}

		public bool IsOut {
			get { return (attributes & ParameterAttributes.Out) != 0; }
			set {
				if (value)
					attributes |= ParameterAttributes.Out;
				else
					attributes &= ~ParameterAttributes.Out;
			}
		}

		public bool IsOptional {
			get { return (attributes & ParameterAttributes.Optional) != 0; }
			set {
				if (value)
					attributes |= ParameterAttributes.Optional;
				else
					attributes &= ~ParameterAttributes.Optional;
			}
		}

		public bool HasDefault {
			get { return (attributes & ParameterAttributes.HasDefault) != 0; }
			set {
				if (value)
					attributes |= ParameterAttributes.HasDefault;
				else
					attributes &= ~ParameterAttributes.HasDefault;
			}
		}

		#endregion

		public ParameterDefinition (TypeReference type)
			: this (string.Empty, ParameterAttributes.None, type)
		{
		}

		public ParameterDefinition (string name, ParameterAttributes attributes, TypeReference type)
			: base (name, type)
		{
			this.attributes = attributes;
		}

		public override ParameterDefinition Resolve ()
		{
			return this;
		}
	}
}
